package com.nuaa.controller.admin;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nuaa.controller.base.BaseController;
import com.nuaa.domain.user.customer.CustomerInfo;
import com.nuaa.service.admin.inter.SystemServiceInter;
import com.nuaa.service.inter.CustomServiceInter;
import com.nuaa.utils.AjaxMsg;
import com.nuaa.utils.DataGrid;
import com.nuaa.utils.PageInfo;

@Controller
@RequestMapping("admin/system")
public class SystemController extends BaseController{
	
	@Autowired
	private SystemServiceInter systemService;
	
	@Autowired
	private CustomServiceInter customService;
	
	/**
	 * 显示客户管理主页面
	 * @return
	 */
	@RequestMapping(value="/customer_manage",method = RequestMethod.GET)
	public String customerManage(){		
		return "admin/system/customer_manage";
	}
	
	/**
	 * 分页显示用户
	 * @param pager
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="customer_manage",params = "method=index",method = RequestMethod.POST)
	public DataGrid<CustomerInfo> customerManageIndex(PageInfo pager, CustomerInfo criteriaCustom){
		try {
			return systemService.getAll(pager,criteriaCustom);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 管理员添加用户，默认密码123
	 * @param custom
	 * @return
	 */
	@ResponseBody
	@RequestMapping("customer_manage/add.do")
	public void customerManageAdd(CustomerInfo custom,HttpServletResponse response){
		AjaxMsg msg = new AjaxMsg();
		try {
			customService.add(custom);
			msg.setSuccess(true);
			msg.setMsg("添加成功！");
			this.writeJson(response, msg);
		} catch (Exception e1) {
			msg.setMsg(e1.getMessage());
			msg.setSuccess(false);
			this.writeJson(response, msg);
		}
	}
	
	/**
	 * 根据主键数组删除用户信息
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="customer_manage",params="method=delete",method=RequestMethod.POST)
	public void customerManageDelete(Integer[] ids,HttpServletResponse response){
		AjaxMsg msg = new AjaxMsg();
		try {
			customService.delete(ids);
			msg.setSuccess(true);
			msg.setMsg("删除成功!");
			this.writeJson(response, msg);
		} catch (Exception e) {
			msg.setMsg(e.getMessage());
			msg.setSuccess(false);
			this.writeJson(response, msg);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="customer_manage",params="method=show",method=RequestMethod.GET)
	public CustomerInfo customerShow(Integer id){
		CustomerInfo info = customService.getById(id);
		return info;
		
	}
	
	@ResponseBody
	@RequestMapping(value="customer_manage",params="method=update",method=RequestMethod.POST)
	public void customerUpdate(CustomerInfo customer,HttpServletResponse response){
		AjaxMsg msg = new AjaxMsg();
		try{
			customService.updateInfo(customer);
			msg.setMsg("更新成功！");
			msg.setSuccess(true);
			this.writeJson(response, msg);
			
		}catch(Exception e){
			e.printStackTrace();
			
			msg.setMsg(e.getMessage());
			msg.setSuccess(false);
			this.writeJson(response, msg);
		}
		
	}
	
	/**
	 * 获取所有客户编码
	 * heaven夏
	 */
	@RequestMapping("/allCustomerNos.do")
	public void getAllCustomerNos(HttpServletResponse response){
		this.writeJson(response,customService.getAllCustomerNos());
	}
	/**
	 * 重置用户密码
	 * @param response
	 * @param ids
	 */
	@RequestMapping("/resetPass.do")
	public void resetPass(HttpServletResponse response,String ids){
		this.writeJson(response, customService.resetPass(ids));
	}
}
